package com.pda.yuanjing.pages.smqc;

import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.pda.yuanjing.R;
import com.pda.yuanjing.boardcast.CustomReceiver;
import com.pda.yuanjing.boardcast.ScannerInterface;
import com.pda.yuanjing.tools.StringUtils;
import com.public_module.basemvp.activity.BaseActivity;
import com.public_module.tools.StringProcess;
import com.public_module.tools.VoiceUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CodeClearActivity extends BaseActivity<CodeClearModel, CodeClearView, CodeClearPresent> implements CodeClearView {


    @BindView(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tvr)
    TextView tvr;
    @BindView(R.id.rltitle)
    RelativeLayout rltitle;
    @BindView(R.id.oet)
    EditText oet;
    @BindView(R.id.tvq)
    TextView tvq;
    @BindView(R.id.bt)
    Button bt;
    private String TAG = "CodeClearActivity";

    ScannerInterface scanner;
    CustomReceiver customReceiver = null;
    VoiceUtils voiceUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clear);
        ButterKnife.bind(this);
        tvTitle.setText("数码清除");
        scanner = new ScannerInterface(this);
        scanner.setOutputMode(1);
        customReceiver = new CustomReceiver(
                new CustomReceiver.PListener1() {
                    @Override
                    public void setData(CustomReceiver.Dataa dataa) {
                    }
                },
                new CustomReceiver.PListener2() {
                    @Override
                    public void setText(String string) {
                        String ss = StringProcess.codeMat(string);
                        presenter.codeReplace(ss);
                    }
                });
        IntentFilter filter = new IntentFilter();
        filter.addAction(ScannerInterface.RES_ACTION);
        registerReceiver(customReceiver, filter);
        voiceUtils = new VoiceUtils(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (customReceiver != null) {
            unregisterReceiver(customReceiver);
        }
        if (scanner != null) {
            scanner.setOutputMode(0);
        }
    }

    @Override
    public CodeClearModel createModel() {
        return new CodeClearModelImpl(this);
    }

    @Override
    public CodeClearView createView() {
        return this;
    }

    @Override
    public CodeClearPresent createPresenter() {
        return new CodeClearPresent();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void tip(String str) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tvq.setText(str);
                voiceUtils.voiceWarn();
            }
        });
    }

    @Override
    public void tipSuccess(String str) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                oet.setText("");
                tvq.setText(str);
            }
        });
    }

    @OnClick({R.id.iv_title_left, R.id.bt})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.bt:
                presenter.codeReplace(oet.getText().toString());
                break;
        }
    }
}
